package com.neusoft.neuiotms.dcam.fixed.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.fixed.dto.ContractDTO;
import com.neusoft.neuiotms.dcam.fixed.model.Contract;
import com.neusoft.neuiotms.dcam.fixed.repository.ContractRepository;

@Component
public class ContractConverter extends AbstractConverter<Contract, ContractDTO> {

    @Autowired
    private ContractRepository contractRepository;

    @Override
    public Contract toModel(final ContractDTO dto) {
        Contract model;

        if (dto.isNew()) {
            model = new Contract();
        } else {
            model = this.contractRepository.getOne(dto.getId());
        }

        model.setCode(dto.getCode());
        model.setName(dto.getName());
        model.setManufacturer(dto.getManufacturerName());
        model.setSigningDate(dto.getSigningDate());
        model.setDescription(dto.getDescription());
        model.setPerson(dto.getManufacturerContact());
        model.setPhone(dto.getManufacturerTelephone());
        // 供货商设置
        //        if (null != dto.getManufacturerId()) {
        //            final Manufacturer manufacturer = this.manufacturerRepository.getOne(dto.getManufacturerId());
        //            model.setManufacturer(manufacturer);
        //        }
        model.setAttachment(dto.getAttachment());
        return model;
    }

    @Override
    public ContractDTO toDTO(final Contract model, final boolean forListView) {

        final ContractDTO dto = new ContractDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setSigningDate(model.getSigningDate());
        dto.setManufacturerContact(model.getPerson());
        dto.setManufacturerName(model.getManufacturer());
        dto.setManufacturerTelephone(model.getPhone());
        dto.setDescription(model.getDescription());
        dto.setAttachment(model.getAttachment());
        return dto;
    }

}
